/*
 * Copyright (C) 2000-2007 Wang Pengcheng <wpc0000@gmail.com>
 * Licensed to the Wang Pengcheng under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The LGPL licenses this file to You under the GNU Lesser General Public
 * Licence, Version 2.0  (the "License"); you may not use this file except in
 * compliance with the License.  You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/lgpl.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//28 Aug 2007
package cn.edu.whu.control;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import java.util.Iterator;

import cn.edu.whu.data.DataList;
import cn.edu.whu.data.VarList;

/**
 * Edit the template's DatabaseBean
 * 
 * 
 * @author wpc
 * @version 0.0.1
 * 
 */
public class EditBean {

	private static String fileUrl = BasicDataForEdit.TEMPLATE_FILE_DIR+"database/DateBaseBean.java";

	private static String SALE_FILE_NAME = "test.java";

	private static HashMap<String, String> mapTemp = new HashMap<String, String>();

	private static HashMap<String, String> map = new HashMap<String, String>();

	private VarList var;

	private DataList list;

	public EditBean(VarList var, DataList list) throws Exception {
		
		this.var = var;
		this.list = list;
		
		mapTemp.clear();
		map.clear();

	}

	public void add() {
		String[] st = { "begin", "end" };
		for (int p = 0; p < st.length; p++) {
			String s = mapTemp.get(st[p]);
			for (int i = 0; i < var.length(); i++) {
				String regex = var.getVar(i);
				String replacement = var.getValue(i);
				s=s.replace(regex, replacement);
			}
			map.put(st[p], map.get(st[p]) + s);
		}

		st[0] = "attribute";
		st[1] = "method";
		for (int p = 0; p < st.length; p++) {
			for (int i = 0; i < list.size(); i++) {
				String s = mapTemp.get(st[p]);
				var.setValue("$TYPE", (list.get(i)).getType());
				var.setValue("$NAME", (list.get(i)).getName());
				var.setValue("$METHODNAME", (list.get(i)).getMethodName());
				for (int c = 0; c < var.length(); c++) {
					String regex = var.getVar(c);
					String replacement = var.getValue(c);
					s=s.replace(regex, replacement);
				}
				map.put(st[p], map.get(st[p]) + s);
			}
		}
	}

	public void read() throws IOException {
		
		//InputStream storageFile = getClass().getResourceAsStream(fileUrl);

		
		FileInputStream storageFile = new FileInputStream(fileUrl);
		BufferedReader in = new BufferedReader(new InputStreamReader(
				storageFile));
		
		String i = "";
		String k = "";

		while (true) {
			String s = in.readLine();
			if (s == null) {
				mapTemp.put(k, i);
				break;
			}

			if (s.contains("//<!--")) {
				if (!(k.equals(""))) {
					mapTemp.put(k, i);
				}
				i = "";
				k = s.substring(s.indexOf("//<!--") + 6);
				map.put(k, s + "\n");
			} else {
				i += s + "\n";
			}
		}
	}
	
	public void read(String url)throws Exception{
		fileUrl=url;
		read();
	}

	public String toString() {
		String s = "";

		Iterator<String> it = (map.keySet()).iterator();
		while (it.hasNext()) {
			String i = it.next();
			s += (i + "=" + mapTemp.get(i)) + "\n";
		}

		return s;
	}

	public void write() throws Exception {

		FileOutputStream saleFile = new FileOutputStream(SALE_FILE_NAME);
		BufferedWriter out = new BufferedWriter(
				new OutputStreamWriter(saleFile));
		out.flush();
		out.newLine();
		out.append(map.get("begin"));
		out.append(map.get("attribute"));
		out.append(map.get("method"));
		out.append(map.get("end"));
		out.close();
	}
	public void setPath(String url){
		SALE_FILE_NAME=url;
	}
}
